package com.xxxx.service.impl;

import com.xxxx.pojo.Order;
import com.xxxx.pojo.Product;
import com.xxxx.service.OrderService;
import com.xxxx.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


@Service
public class OrderServiceImpl implements OrderService {

	@Autowired
	RestTemplate restTemplate;

	@Autowired
	ProductService productService;

	@Override
	public Map<Object, Object> createP(Product product) {
		return productService.createP(product);
	}

	@Override
	public Order selectOr(Integer id) {
		//return new Order(1,selectP1());
		return new Order(1,Arrays.asList(productService.selectById(id)));
	}

	private List<Product> selectP1(){
		return productService.selectP();
	}

	private List<Product> selectP(){
		ResponseEntity<List<Product>> responseEntity = restTemplate.exchange(
				"http://service-provider/product/list",
				HttpMethod.GET,
				null,
				new ParameterizedTypeReference<List<Product>>() {
				});
		return responseEntity.getBody();
	}
}
